package cn.edu.xaut.util;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class JDBCUtils {
    public static Connection getConnection() throws SQLException,ClassNotFoundException{

        Class.forName( "com.mysql.jdbc.Driver" );
        //获取数据库连接
        String url="jdbc:mysql://127.0.0.1:3306/Mycake?serverTimezone=GMT%2B8";
        String DBname = "root";
        String DBpassword = "030318";
        Connection conn=java.sql.DriverManager.getConnection(url,DBname,DBpassword);
        return conn;
    }
    //关闭数据库连接
    public static void release(Statement statement, Connection conn) {
        if(statement != null){
            try{
                statement.close();
            }catch (SQLException e){
                e.printStackTrace();
            }
            statement = null;
        }
        if(conn!=null){
            try{
                conn.close();
            }catch (SQLException e){
                e.printStackTrace();
            }
            conn = null;
        }
    }

    public static void release(ResultSet resultSet,Statement statement,Connection conn){
        if(resultSet != null){
            try{
                resultSet.close();
            }catch (SQLException e){
                e.printStackTrace();
            }
            resultSet = null;
        }
        release(statement,conn);
    }
}
